***********************************************
*This file reshapes graduations from the IPEDS*
***********************************************

local yearList 1989 1990 1991 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015

* reshape 
foreach i in `yearList' {
use "$clean_data_education/completions.dta"
keep if year == `i'
drop cipcode 

gen STEM = 0 
replace STEM = 1 if majors == 1 // agricultural sciences
replace STEM = 1 if majors == 3 // Natural resources and conservation
replace STEM = 1 if majors == 11 // Computer science
replace STEM = 1 if majors == 14 // Engineering
replace STEM = 1 if majors == 15 // Engineering Technologies
replace STEM = 1 if majors == 26 // Biological sciences
replace STEM = 1 if majors == 27 // Math
replace STEM = 1 if majors == 30 // Multi/interdisciplinary studies
replace STEM = 1 if majors == 40 // Physical science
replace STEM = 1 if majors == 41 // Science Technologies
replace STEM = 1 if majors == 52 // Management Sciences and Quantitative Methods, Other
replace STEM = 2 if majors == 0 // Institution total
replace STEM = 2 if majors == 99 // Institution total

* group some degrees
gen awardGroup = .
replace awardGroup = awlevel if awlevel <= 5
replace awardGroup = 6 if awlevel > 5 
label value awardGroup label_awlevel
drop if awardGroup == .
drop if awardGroup < 0
drop awlevel

collapse (sum) blackMen blackWomen hispanicMen hispanicWomen whiteMen ///
whiteWomen men women black hispanic white total (last) year, by(unitid awardGroup STEM)

egen reshapeGroup = concat(awardGroup STEM)
drop STEM awardGroup

reshape wide blackMen blackWomen hispanicMen hispanicWomen whiteMen ///
whiteWomen men women black hispanic white total, i(unitid) j(reshapeGroup) string


*labels 
foreach v in blackMen blackWomen hispanicMen hispanicWomen whiteMen whiteWomen men women black hispanic white total {
label variable `v'10 "NON-STEM, Award of less than 1 academic year" 
replace `v'10 = 0 if `v'10 == . // because of reshape...
label variable `v'11 "STEM, Award of less than 1 academic year" 
replace `v'11 = 0 if `v'11 == .
label variable `v'12 "Award of less than 1 academic year (STEM+NON-STEM)"
replace `v'12 = 0 if `v'12 == .
label variable `v'20 "NON-STEM, Award of at least 1 but less than 2 academic year" 
replace `v'20 = 0 if `v'20 == .
label variable `v'21 "STEAM, Award of at least 1 but less than 2 academic year" 
replace `v'21 = 0 if `v'21 == .
label variable `v'22 "Award of at least 1 but less than 2 academic year (STEM+NON-STEM)" 
replace `v'22 = 0 if `v'22 == .
label variable `v'30 "NON-STEM, Associates Degree"
replace `v'30 = 0 if `v'30 == .
label variable `v'31 "STEM, Associates Degree"
replace `v'31 = 0 if `v'31 == .
label variable `v'32 "Associates Degree (STEM+NONSTEM)"
replace `v'32 = 0 if `v'32 == .
label variable `v'40 "NON-STEM, Award of at least 2 but less than 4 academic year" 
replace `v'40 = 0 if `v'40 == .
label variable `v'41 "STEM, Award of at least 2 but less than 4 academic year" 
replace `v'41 = 0 if `v'41 == .
label variable `v'42 "Award of at least 2 but less than 4 academic year (STEM+NONSTEM)"
replace `v'42 = 0 if `v'42 == .
label variable `v'50 "NON-STEM, Bachelor's"
replace `v'50 = 0 if `v'50 == .
label variable `v'51 "STEM, Bachelor's"
replace `v'51 = 0 if `v'51 == .
label variable `v'52 "Bachelor's, (STEM+NONSTEM)"
replace `v'52 = 0 if `v'52 == .
label variable `v'60 "STEM, postbaccalaureate (graduate, phd, first-professional...)"
replace `v'60 = 0 if `v'60 == .
label variable `v'61 "NON-STEM, postbaccalaureate (graduate, phd, first-professional...)"
replace `v'61 = 0 if `v'61 == .
label variable `v'62 "postbaccalaureate (graduate, phd, first-professional...) (STEM+NONSTEM)"
replace `v'62 = 0 if `v'62 == .

}

save "$clean_data_education/Temporary3_`i'.dta", replace
}

* append
use "$clean_data_education/temporary3_1989.dta", clear
foreach i in 1990 1991 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015 {
append using "$clean_data_education/temporary3_`i'.dta"
rm "$clean_data_education/temporary3_`i'.dta"
}
rm "$clean_data_education/temporary3_1989.dta"

*** Build three year averages
* year group
gen yearGroup = 0
	replace yearGroup = 1990 if year <= 1991 
	replace yearGroup = 1994 if year >= 1992 & year <= 1994
	replace yearGroup = 2000 if year >= 1999 & year <= 2001
	replace yearGroup = 2007 if year >= 2006 & year <= 2008
	replace yearGroup = 2014 if year >= 2013
	drop if yearGroup == 0

**********************************************************
* Run it all together, save labels and collapse
foreach v of var * {
local l`v' : variable label `v'
	if `"`l`v''"' == "" {
	local l`v' "`v'"
  	}
}
collapse (mean) blackMen10-total62, by(unitid yearGroup)

foreach v of var * {
label var `v' "`l`v''"
}
**********************************************************

keep unitid year total* men* women*
save "$clean_data_education/completionsAwardedTotalsReshaped.dta", replace


